package com.mimc.service;

import com.alibaba.fastjson.JSON;
import com.jfinal.aop.Inject;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.mimc.common.constant.CacheConstants;
import com.mimc.core.model.SysConfig;
import com.mimc.core.model.SysLang;
import com.mimc.core.service.SysConfigService;
import com.mimc.core.service.SysLangService;
import com.mimc.core.service.SysTranslateService;
import com.mimc.framework.domain.KV;
import io.jboot.aop.annotation.Bean;
import io.jboot.db.model.Columns;
import io.jboot.utils.CacheUtil;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@Bean
public class ShTranslateService {
    @Inject
    private SysLangService sysLangService;
    @Inject
    private SysTranslateService sysTranslateService;
    @Inject
    private ShSysConfigService shSysConfigService;
    @Inject
    private SysConfigService sysConfigService;
    public void loadLangCache(){
        List<SysLang> list=sysLangService.findAll();
        CacheUtil.put(CacheConstants.LANG,CacheConstants.LANG_LIST,list);
        loadTranslateCache(list);
        //版本+1操作
        String langVersion=shSysConfigService.getConfigValueByKey("lang.version");
        Integer version=Integer.parseInt(langVersion);
        SysConfig sysConfig=sysConfigService.findFirstByColumns(Columns.create("config_key","lang.version"));
        if(sysConfig!=null){
            version++;
            sysConfig.setConfigValue(version+"");
            shSysConfigService.updateConfig(sysConfig);
        }
    }
    public List<SysLang> getLangListByCache(){
        return CacheUtil.get(CacheConstants.LANG,CacheConstants.LANG_LIST);
    }
    public List<KV> getTranslateByLangName(String varName){
       return CacheUtil.get(CacheConstants.TANSLATE,varName);
    }
    public String getDefaultLang(){
        List<SysLang> langList=getLangListByCache();
        for (int i=0;i<langList.size();i++){
            SysLang sysLang=langList.get(i);
            if(sysLang.getIsDefault()){
                return sysLang.getVarName();
            }
        }
        return "";
    }
    public String translate(String key,String lang){
        List<KV> list=getTranslateByLangName(lang);
        if(list==null){
            return key;
        }
        for(int i=0;i<list.size();i++){
            KV kv=list.get(i);
            if(kv.getKey().equals(key)){
                return kv.getValue();
            }
        }
        return key;
    }
    private void loadTranslateCache(List<SysLang> langList){
        List<Record> translateList= Db.findAll("sys_translate");
        for (int i=0;i<langList.size();i++){
            SysLang lang=langList.get(i);
            List<KV> kvList=getTranslateKvList(translateList,lang.getVarName());
            CacheUtil.put(CacheConstants.TANSLATE,lang.getVarName(),kvList);
        }
    }
    private List<KV> getTranslateKvList(List<Record> translateList,String langName){
        List<KV> kvList=new ArrayList<>();
        for (int i=0;i<translateList.size();i++){
            Record translate=translateList.get(i);
            KV kv=new KV();
            kv.setKey(translate.get("translate_key"));
            kv.setValue(translate.get(langName));
            kvList.add(kv);
        }
        return kvList;
    }
}
